import 'package:flutter/material.dart';

class ToastMenu {
  static void showToast(
    BuildContext context,
    String message, {
    Color? backgroundColor,
    Duration duration = const Duration(milliseconds: 1000),
    double? topOffset,
    Color textColor = Colors.white,
    double fontSize = 16,
    FontWeight fontWeight = FontWeight.w500,
  }) {
    if (!context.mounted) return;

    final overlay = Overlay.of(context);
    late OverlayEntry overlayEntry;

    overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        top: topOffset ?? MediaQuery.of(context).size.height * 0.4,
        left: MediaQuery.of(context).size.width * 0.15,
        right: MediaQuery.of(context).size.width * 0.15,
        child: Material(
          color: Colors.transparent,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
            decoration: BoxDecoration(
              color: backgroundColor ?? Color(0xFF2C2C2C),
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.black26,
                  blurRadius: 10,
                  offset: Offset(0, 4),
                ),
              ],
            ),
            child: FittedBox(
              fit: BoxFit.scaleDown,
              child: Text(
                message,
                textAlign: TextAlign.center,
                maxLines: 1,
                softWrap: false,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: textColor,
                  fontSize: fontSize,
                  fontWeight: fontWeight,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    overlay.insert(overlayEntry);

    // 自动移除Toast
    Future.delayed(duration, () {
      if (overlayEntry.mounted) {
        overlayEntry.remove();
      }
    });
  }

  // 带标题和内容的Toast
  static void showToastWithTitle(
    BuildContext context,
    String title,
    String content, {
    Color? backgroundColor,
    Duration duration = const Duration(milliseconds: 5000),
    double? topOffset,
    Color textColor = Colors.white,
    double titleFontSize = 18,
    double contentFontSize = 14,
    FontWeight titleFontWeight = FontWeight.bold,
    FontWeight contentFontWeight = FontWeight.w400,
    int maxTitleLines = 2,
    int maxContentLines = 3,
  }) {
    if (!context.mounted) return;

    final overlay = Overlay.of(context);
    late OverlayEntry overlayEntry;

    overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        top: topOffset ?? MediaQuery.of(context).size.height * 0.35,
        left: MediaQuery.of(context).size.width * 0.1, // 稍微增加宽度来容纳更多内容
        right: MediaQuery.of(context).size.width * 0.1,
        child: Material(
          color: Colors.transparent,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 24, vertical: 20),
            decoration: BoxDecoration(
              color: backgroundColor ?? Color(0xFF2C2C2C),
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.black26,
                  blurRadius: 10,
                  offset: Offset(0, 4),
                ),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // 标题
                Text(
                  title,
                  textAlign: TextAlign.center,
                  maxLines: maxTitleLines,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: textColor,
                    fontSize: titleFontSize,
                    fontWeight: titleFontWeight,
                  ),
                ),
                if (content.isNotEmpty) ...[
                  SizedBox(height: 8),
                  // 内容
                  Text(
                    content,
                    textAlign: TextAlign.center,
                    maxLines: maxContentLines,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: textColor,
                      fontSize: contentFontSize,
                      fontWeight: contentFontWeight,
                    ),
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );

    overlay.insert(overlayEntry);

    // 自动移除Toast
    Future.delayed(duration, () {
      if (overlayEntry.mounted) {
        overlayEntry.remove();
      }
    });
  }
}
